import App from "../../App"
import SCPacket from "./packet/SCPacket"
import CSPacket from "./packet/CSPacket"
import Log from "../../utils/Log"
import PacketBase from "./packet/PacketBase"
import ByteArray from "../../egret/core/utils/ByteArray"
import EgretWebSocket from "../../egret/extension/socket/EgretWebSocket"
/** 二进制的消息 */
export default class ByteArrayMsg implements BaseMsg {

    protected _msgBuffer: ByteArray

    /**
     * 构造函数
     */
    public constructor() {
        this._msgBuffer = new ByteArray()
    }

    /**
     * 接收消息处理
     * @param msg
     */
    public receive(netChannelName: string, packet: SCPacket): void {
        let obj: any = this.decode(packet)
        if (obj) {
            App.MessageCenter.dispatch(obj.key, obj.body)
        }

        //TODO double bytearray clear
        if (this._msgBuffer.bytesAvailable == 0) {
            this._msgBuffer.clear()
        }
    }

    /**
     * 发送消息处理
     * @param msg
     */
    public send(netChannelName: string, msg: CSPacket): void {
        let socket: EgretWebSocket = App.Net.getNetChannel(netChannelName).getSocket()
        let obj: any = this.encode(msg)
        if (obj) {
            obj.position = 0
            socket.writeBytes(obj, 0, obj.bytesAvailable)
            socket.flush()
        }
    }

    /**
     * 消息解析
     * @param msg
     */
    public decode(msg: any): any {
        Log.info("decode需要子类重写，根据项目的协议结构解析")
        return null
    }

    /**
     * 消息封装
     * @param msg
     */
    public encode(msg: any): any {
        Log.info("encode需要子类重写，根据项目的协议结构解析")
        return null
    }

    /** 获取数据包中的协议Id
   * @param msg 数据包
  */
    public getProtocolId(msg: PacketBase): string {
        Log.info("getProtocolId需要子类重写，根据项目的协议结构解析")
        return null
    }
}